package com.xiaodou.mjf.web.console.basic.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.basic.service.MenuService;
import com.xiaodou.mjf.application.basic.utils.OperatorUtils;
import com.xiaodou.mjf.entity.basic.Menu;
import com.xiaodou.mjf.entity.basic.Operator;
import com.xiaodou.mjf.entity.basic.Role;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;
import com.xiaodou.mjf.web.console.basic.vo.MenuZTree;

@Controller
@RequestMapping("menu")
public class MenuController extends ApplicationController {

    @Resource
    MenuService menuService;

    @RequiresPermissions("sys:menu:view")
    @RequestMapping(value = {"", "list"})
    public String list(Model model) {

        List<Menu> sourceMenus = OperatorUtils.getMenuList();
        List<Menu> menus = Lists.newArrayList();
        Menu.sort(menus, sourceMenus, Menu.ROOT_ID);

        model.addAttribute("menus", menus);
        model.addAttribute("platform", Menu.PLATFORM_CONSOLE);

        return "security/menu/list";
    }

    @RequestMapping(value = {"front"})
    public String frontMenus(Model model) {

        List<Menu> sourceMenus = menuService.getFrontMenus(new Menu());
        List<Menu> menus = Lists.newArrayList();
        Menu.sort(menus, sourceMenus, Menu.FRONT_ROOT_ID);

        model.addAttribute("menus", menus);
        model.addAttribute("platform", Menu.PLATFORM_PORTAL);

        return "security/menu/list";
    }
    
    @RequestMapping(value = {"merchant"})
    public String merchantMenus(Model model) {

        List<Menu> sourceMenus = menuService.getMerchantMenus(new Menu());
        List<Menu> menus = Lists.newArrayList();
        Menu.sort(menus, sourceMenus, Menu.MERACHANT_ROOT_ID);

        model.addAttribute("menus", menus);
        model.addAttribute("platform", Menu.PLATFORM_MERACHANT);

        return "security/menu/list";
    }

    @RequestMapping(value = {"form", "form/{id}", "add/{parent.id}"})
    public String form(Menu menu, Model model, @PathVariable String id) {
        if (StringUtils.isNotBlank(menu.getId())) {
            menu = menuService.get(menu.getId());
        }
        if (menu.getParent() == null || menu.getParent().getId() == null) {
            menu = new Menu(new Menu(id));
        }

        model.addAttribute("menu", menu);
        return "security/menu/form";
    }

    @RequiresPermissions("sys:menu:edit")
    @RequestMapping("save")
    public String save(Menu menu, Model model) {
        if (!beanValidator(model, menu)) {
            return form(menu, model, null);
        }

        menuService.save(menu);

        return menu.getPlatform().equals(Menu.PLATFORM_CONSOLE) ? "redirect:/menu/list" : "redirect:/menu/front";
    }

    @RequestMapping("findByParentId")
    @ResponseBody
    public List<MenuZTree> list(@RequestParam(value = "id", required = false) String parentId) {
    	Operator operator = OperatorUtils.getOperator();
    	List<MenuZTree> result = Lists.newArrayList();
    	if (StringUtils.isBlank(parentId)) {
            parentId = "0";
        }
        
    	if(operator.getId().equals("1")) {
            List<Menu> menuList = menuService.findByParentId(parentId);
            for (Menu menu : menuList) {
            	if(!parentId.equals("2")) {
            		 MenuZTree menuZTree = new MenuZTree();
                     menuZTree.setId(menu.getId());
                     menuZTree.setName(menu.getName());
                     List<Menu> menuList2 = menuService.findByParentId(menu.getId());
                     if (menuList2.size() > 0) {
                         menuZTree.setIsParent("true");
                     } else {
                         menuZTree.setIsParent("false");
                     }
                     result.add(menuZTree);
            	}
            }
    	} else {
    		List<Menu> menuList = menuService.findByParentId(parentId);
            for (Menu menu : menuList) {
            	if(parentId.equals("0") || parentId.equals(operator.getRoles().get(0).getPlatform().toString())) {
            		 MenuZTree menuZTree = new MenuZTree();
                     menuZTree.setId(menu.getId());
                     menuZTree.setName(menu.getName());
                     List<Menu> menuList2 = menuService.findByParentId(menu.getId());
                     if (menuList2.size() > 0) {
                         menuZTree.setIsParent("true");
                     } else {
                         menuZTree.setIsParent("false");
                     }
                     result.add(menuZTree);
            	}
            }
    	}
    	
    	return result;
    	}
}
